using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
	public partial class DiemHocPhan
	{
		#region Properties.
		
		public string MaSV { set; get; }
		public string Lop { set; get; }
		public Guid ModuleID { set; get; }
		public int HeDaoTaoID { set; get; }
		public int HocPhanID { set; get; }
		public int HocKyID { set; get; }
		public int NamHocID { set; get; }
		public double DiemThi { set; get; }
		public double DiemTongKet { set; get; }
		public string GhiChu { set; get; }
		public string CreatedByUser { set; get; }
		public DateTime DateCreated { set; get; }
		public string ModifiedByUser { set; get; }
		public DateTime DateModified { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static DiemHocPhan Load(string maSV)
		{
			const string spName = "[dbo].[p_CDTM_DiemHocPhan_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.VarChar, maSV);
			DiemHocPhan entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new DiemHocPhan();
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HeDaoTaoID"))) entity.HeDaoTaoID = reader.GetInt32(reader.GetOrdinal("HeDaoTaoID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocPhanID"))) entity.HocPhanID = reader.GetInt32(reader.GetOrdinal("HocPhanID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocKyID"))) entity.HocKyID = reader.GetInt32(reader.GetOrdinal("HocKyID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamHocID"))) entity.NamHocID = reader.GetInt32(reader.GetOrdinal("NamHocID"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemThi"))) entity.DiemThi = reader.GetDouble(reader.GetOrdinal("DiemThi"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTongKet"))) entity.DiemTongKet = reader.GetDouble(reader.GetOrdinal("DiemTongKet"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedByUser"))) entity.CreatedByUser = reader.GetString(reader.GetOrdinal("CreatedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateCreated"))) entity.DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModifiedByUser"))) entity.ModifiedByUser = reader.GetString(reader.GetOrdinal("ModifiedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateModified"))) entity.DateModified = reader.GetDateTime(reader.GetOrdinal("DateModified"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<DiemHocPhan> SelectCollectionAll(Guid moduleID)
		{
			List<DiemHocPhan> collection = new List<DiemHocPhan>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll(moduleID);
			while (reader.Read())
			{
				DiemHocPhan entity = new DiemHocPhan();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HeDaoTaoID"))) entity.HeDaoTaoID = reader.GetInt32(reader.GetOrdinal("HeDaoTaoID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocPhanID"))) entity.HocPhanID = reader.GetInt32(reader.GetOrdinal("HocPhanID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocKyID"))) entity.HocKyID = reader.GetInt32(reader.GetOrdinal("HocKyID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamHocID"))) entity.NamHocID = reader.GetInt32(reader.GetOrdinal("NamHocID"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemThi"))) entity.DiemThi = reader.GetDouble(reader.GetOrdinal("DiemThi"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTongKet"))) entity.DiemTongKet = reader.GetDouble(reader.GetOrdinal("DiemTongKet"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedByUser"))) entity.CreatedByUser = reader.GetString(reader.GetOrdinal("CreatedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateCreated"))) entity.DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModifiedByUser"))) entity.ModifiedByUser = reader.GetString(reader.GetOrdinal("ModifiedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateModified"))) entity.DateModified = reader.GetDateTime(reader.GetOrdinal("DateModified"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<DiemHocPhan> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<DiemHocPhan> collection = new List<DiemHocPhan>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				DiemHocPhan entity = new DiemHocPhan();
				
				if (!reader.IsDBNull(reader.GetOrdinal("MaSV"))) entity.MaSV = reader.GetString(reader.GetOrdinal("MaSV"));
				if (!reader.IsDBNull(reader.GetOrdinal("Lop"))) entity.Lop = reader.GetString(reader.GetOrdinal("Lop"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HeDaoTaoID"))) entity.HeDaoTaoID = reader.GetInt32(reader.GetOrdinal("HeDaoTaoID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocPhanID"))) entity.HocPhanID = reader.GetInt32(reader.GetOrdinal("HocPhanID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HocKyID"))) entity.HocKyID = reader.GetInt32(reader.GetOrdinal("HocKyID"));
				if (!reader.IsDBNull(reader.GetOrdinal("NamHocID"))) entity.NamHocID = reader.GetInt32(reader.GetOrdinal("NamHocID"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemThi"))) entity.DiemThi = reader.GetDouble(reader.GetOrdinal("DiemThi"));
				if (!reader.IsDBNull(reader.GetOrdinal("DiemTongKet"))) entity.DiemTongKet = reader.GetDouble(reader.GetOrdinal("DiemTongKet"));
				if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedByUser"))) entity.CreatedByUser = reader.GetString(reader.GetOrdinal("CreatedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateCreated"))) entity.DateCreated = reader.GetDateTime(reader.GetOrdinal("DateCreated"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModifiedByUser"))) entity.ModifiedByUser = reader.GetString(reader.GetOrdinal("ModifiedByUser"));
				if (!reader.IsDBNull(reader.GetOrdinal("DateModified"))) entity.DateModified = reader.GetDateTime(reader.GetOrdinal("DateModified"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_CDTM_DiemHocPhan_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_DiemHocPhan_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_CDTM_DiemHocPhan_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_CDTM_DiemHocPhan_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertDiemHocPhan(string lop, Guid moduleID, int heDaoTaoID, int hocPhanID, int hocKyID, int namHocID, double diemThi, double diemTongKet, string ghiChu, string createdByUser, DateTime dateCreated, string modifiedByUser, DateTime dateModified)
		{
			DiemHocPhan entity = new DiemHocPhan();	
			entity.Lop = lop;
			entity.ModuleID = moduleID;
			entity.HeDaoTaoID = heDaoTaoID;
			entity.HocPhanID = hocPhanID;
			entity.HocKyID = hocKyID;
			entity.NamHocID = namHocID;
			entity.DiemThi = diemThi;
			entity.DiemTongKet = diemTongKet;
			entity.GhiChu = ghiChu;
			entity.CreatedByUser = createdByUser;
			entity.DateCreated = dateCreated;
			entity.ModifiedByUser = modifiedByUser;
			entity.DateModified = dateModified;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_CDTM_DiemHocPhan_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.VarChar, MaSV);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@HeDaoTaoID", SqlDbType.Int, HeDaoTaoID);
			db.AddInParameter(dbCommand, "@HocPhanID", SqlDbType.Int, HocPhanID);
			db.AddInParameter(dbCommand, "@HocKyID", SqlDbType.Int, HocKyID);
			db.AddInParameter(dbCommand, "@NamHocID", SqlDbType.Int, NamHocID);
			db.AddInParameter(dbCommand, "@DiemThi", SqlDbType.Float, DiemThi);
			db.AddInParameter(dbCommand, "@DiemTongKet", SqlDbType.Float, DiemTongKet);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			db.AddInParameter(dbCommand, "@CreatedByUser", SqlDbType.NVarChar, CreatedByUser);
			db.AddInParameter(dbCommand, "@DateCreated", SqlDbType.DateTime, DateCreated.Year <= 1753 ? DBNull.Value : (object) DateCreated);
			db.AddInParameter(dbCommand, "@ModifiedByUser", SqlDbType.NVarChar, ModifiedByUser);
			db.AddInParameter(dbCommand, "@DateModified", SqlDbType.DateTime, DateModified.Year <= 1753 ? DBNull.Value : (object) DateModified);
			
			if (transaction != null)
			{
				return db.ExecuteNonQuery(dbCommand, transaction);
			}
            else
			{
				return db.ExecuteNonQuery(dbCommand);
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<DiemHocPhan> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (DiemHocPhan item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateDiemHocPhan(string maSV, string lop, Guid moduleID, int heDaoTaoID, int hocPhanID, int hocKyID, int namHocID, double diemThi, double diemTongKet, string ghiChu, string createdByUser, DateTime dateCreated, string modifiedByUser, DateTime dateModified)
		{
			DiemHocPhan entity = new DiemHocPhan();			
			entity.MaSV = maSV;
			entity.Lop = lop;
			entity.ModuleID = moduleID;
			entity.HeDaoTaoID = heDaoTaoID;
			entity.HocPhanID = hocPhanID;
			entity.HocKyID = hocKyID;
			entity.NamHocID = namHocID;
			entity.DiemThi = diemThi;
			entity.DiemTongKet = diemTongKet;
			entity.GhiChu = ghiChu;
			entity.CreatedByUser = createdByUser;
			entity.DateCreated = dateCreated;
			entity.ModifiedByUser = modifiedByUser;
			entity.DateModified = dateModified;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_CDTM_DiemHocPhan_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.VarChar, MaSV);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@HeDaoTaoID", SqlDbType.Int, HeDaoTaoID);
			db.AddInParameter(dbCommand, "@HocPhanID", SqlDbType.Int, HocPhanID);
			db.AddInParameter(dbCommand, "@HocKyID", SqlDbType.Int, HocKyID);
			db.AddInParameter(dbCommand, "@NamHocID", SqlDbType.Int, NamHocID);
			db.AddInParameter(dbCommand, "@DiemThi", SqlDbType.Float, DiemThi);
			db.AddInParameter(dbCommand, "@DiemTongKet", SqlDbType.Float, DiemTongKet);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			db.AddInParameter(dbCommand, "@CreatedByUser", SqlDbType.NVarChar, CreatedByUser);
			db.AddInParameter(dbCommand, "@DateCreated", SqlDbType.DateTime, DateCreated.Year == 1753 ? DBNull.Value : (object) DateCreated);
			db.AddInParameter(dbCommand, "@ModifiedByUser", SqlDbType.NVarChar, ModifiedByUser);
			db.AddInParameter(dbCommand, "@DateModified", SqlDbType.DateTime, DateModified.Year == 1753 ? DBNull.Value : (object) DateModified);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<DiemHocPhan> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (DiemHocPhan item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateDiemHocPhan(string maSV, string lop, Guid moduleID, int heDaoTaoID, int hocPhanID, int hocKyID, int namHocID, double diemThi, double diemTongKet, string ghiChu, string createdByUser, DateTime dateCreated, string modifiedByUser, DateTime dateModified)
		{
			DiemHocPhan entity = new DiemHocPhan();			
			entity.MaSV = maSV;
			entity.Lop = lop;
			entity.ModuleID = moduleID;
			entity.HeDaoTaoID = heDaoTaoID;
			entity.HocPhanID = hocPhanID;
			entity.HocKyID = hocKyID;
			entity.NamHocID = namHocID;
			entity.DiemThi = diemThi;
			entity.DiemTongKet = diemTongKet;
			entity.GhiChu = ghiChu;
			entity.CreatedByUser = createdByUser;
			entity.DateCreated = dateCreated;
			entity.ModifiedByUser = modifiedByUser;
			entity.DateModified = dateModified;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_DiemHocPhan_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.VarChar, MaSV);
			db.AddInParameter(dbCommand, "@Lop", SqlDbType.NVarChar, Lop);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@HeDaoTaoID", SqlDbType.Int, HeDaoTaoID);
			db.AddInParameter(dbCommand, "@HocPhanID", SqlDbType.Int, HocPhanID);
			db.AddInParameter(dbCommand, "@HocKyID", SqlDbType.Int, HocKyID);
			db.AddInParameter(dbCommand, "@NamHocID", SqlDbType.Int, NamHocID);
			db.AddInParameter(dbCommand, "@DiemThi", SqlDbType.Float, DiemThi);
			db.AddInParameter(dbCommand, "@DiemTongKet", SqlDbType.Float, DiemTongKet);
			db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);
			db.AddInParameter(dbCommand, "@CreatedByUser", SqlDbType.NVarChar, CreatedByUser);
			db.AddInParameter(dbCommand, "@DateCreated", SqlDbType.DateTime, DateCreated.Year == 1753 ? DBNull.Value : (object) DateCreated);
			db.AddInParameter(dbCommand, "@ModifiedByUser", SqlDbType.NVarChar, ModifiedByUser);
			db.AddInParameter(dbCommand, "@DateModified", SqlDbType.DateTime, DateModified.Year == 1753 ? DBNull.Value : (object) DateModified);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<DiemHocPhan> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (DiemHocPhan item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteDiemHocPhan(string maSV)
		{
			DiemHocPhan entity = new DiemHocPhan();
			entity.MaSV = maSV;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_CDTM_DiemHocPhan_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@MaSV", SqlDbType.VarChar, MaSV);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_CDTM_DiemHocPhan_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<DiemHocPhan> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (DiemHocPhan item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}